import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { DictApi, ToolTypeApi } from '#/api';

import { reactive, ref } from 'vue';

import { DictResp, getAutoCode } from '#/api';

const switchValue = ref('');

// 将字典数据添加到数组中
const sys_yes_no = reactive<DictApi.SelectOption[]>([]);
const mes_mainten_type = reactive<DictApi.SelectOption[]>([]);

DictResp(['sys_yes_no', 'mes_mainten_type']).then((res) => {
  // 将两个数组合并
  if (res.mes_mainten_type) {
    mes_mainten_type.push(...res.mes_mainten_type);
  }
  if (res.sys_yes_no) {
    sys_yes_no.push(...res.sys_yes_no);
  }
});

export function useSearchForm(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入类型编码',
        class: 'mr-5',
      },
      fieldName: 'toolTypeCode',
      label: '类型编码',
      labelClass: 'text-[#606266] font-bold',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入类型名称',
      },
      fieldName: 'toolTypeName',
      label: '类型名称',
      labelClass: 'text-[#606266] font-bold',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: mes_mainten_type,
        placeholder: '请选择保养维护类型',
      },
      fieldName: 'maintenType',
      label: '保养维护类型',
      labelClass: 'text-[#606266] font-bold',
    },
  ];
}

/**
 * 获取编辑表单的字段配置
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'toolTypeId',
      dependencies: {
        triggerFields: ['toolTypeId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'toolTypeCode',
      label: '类型编号',
      rules: 'required',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入类型编号',
      },
      dependencies: {
        triggerFields: ['switch'],
        trigger(values) {
          values.toolTypeCode = switchValue;
        },
      },
    },
    {
      component: 'Switch',
      fieldName: 'switch',
      label: '自动生成',
      formItemClass: 'col-span-1 w-min',
      componentProps: {
        defaultValue: 0,
        onChange: (e: boolean) => {
          if (e) {
            getAutoCode('TOOL_TYPE_CODE').then((res) => {
              switchValue.value = res.data;
            });
          } else {
            switchValue.value = '';
          }
        },
      },
      dependencies: {
        triggerFields: ['toolTypeId'],
        show(values) {
          return !values.toolTypeId && true;
        },
      },
    },
    {
      component: 'Input',
      fieldName: 'toolTypeName',
      label: '类型名称',
      rules: 'required',
      componentProps: {
        placeholder: '请输入类型名称',
      },
      formItemClass: 'col-span-1 col-start-3',
    },
    {
      component: 'RadioGroup',
      defaultValue: 'Y',
      componentProps: {
        name: 'cname',
        options: sys_yes_no,
      },
      fieldName: 'codeFlag',
      label: '是否编码管理',
      formItemClass: 'col-span-1 col-start-1',
    },
    {
      component: 'Select',
      rules: 'required',
      componentProps: {
        allowClear: true,
        filterOption: true,
        options: mes_mainten_type,
        placeholder: '请选择类型',
        showSearch: true,
      },
      fieldName: 'maintenType',
      label: '保养维护类型',
      formItemClass: 'col-span-1',
      dependencies: {
        triggerFields: ['codeFlag'],
        show(values) {
          return values.codeFlag !== 'N';
        },
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'maintenPeriod',
      label: '保养周期',
      componentProps: {
        placeholder: '请输入保养周期',
      },
      formItemClass: 'col-span-1',
      dependencies: {
        triggerFields: ['codeFlag'],
        show(values) {
          return values.codeFlag !== 'N';
        },
      },
    },

    {
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入标准',
        rows: 3,
        showCount: true,
        class: 'w-full',
      },
      fieldName: 'toolTypeStandard',
      label: '标准',
      emptyStateValue: null,
      formItemClass: 'col-start-1 col-span-3 items-start',
    },
  ];
}

/**
 * 获取表格列配置
 * @param onView 表格点击函数
 */
export function useColumns(
  onView: (param: ToolTypeApi.Row) => void,
): VxeTableGridOptions<ToolTypeApi.Row>['columns'] {
  return [
    // { title: '序号', type: 'seq', width: 50 },
    { field: 'toolTypeId', align: 'left', type: 'checkbox', width: 50 },
    {
      field: 'toolTypeCode',
      title: '类型编码',
      cellRender: { name: 'CellButtonInfo', props: { onClick: onView } },
    },
    { field: 'toolTypeName', title: '类型名称' },
    {
      field: 'codeFlag',
      title: '是否编码管理',
      cellRender: { name: 'CellTag' },
    },
    {
      field: 'maintenType',
      title: '保养维护类型',
      formatter: ({ cellValue, row }) => {
        const item = mes_mainten_type.find((item) => item.value === cellValue);
        return row.codeFlag === 'N' ? '无' : item?.label || '无';
      },
    },
    {
      field: 'maintenPeriod',
      title: '保养周期',
      formatter: ({ cellValue, row }) => {
        if (row.codeFlag === 'N') {
          return '无';
        } else if (row.maintenType === 'REGULAR') {
          return `${cellValue}天`;
        } else if (row.maintenType === 'USAGE') {
          return `${cellValue}次`;
        } else {
          return '无';
        }
      },
    },
    {
      field: 'remark',
      title: '备注',
      formatter: ({ cellValue }) => {
        return cellValue || '-';
      },
    },
    {
      field: 'action',
      title: '操作',
      width: 150,
      fixed: 'right',
      align: 'center',
      slots: { default: 'action' },
    },
  ];
}
